package com.lj.back.manage.scaffold.module.kaptcha.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lj.back.manage.scaffold.common.DTO.ValidCodeDTO;
import com.lj.back.manage.scaffold.common.enums.ResponseEnum;
import com.lj.back.manage.scaffold.common.response.R;
import com.lj.back.manage.scaffold.module.kaptcha.KaptchaUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 图形验证码接口
 * @author lv
 * @version 1.0.0
 */
@RestController
@RequestMapping("/kaptcha")
@Api(tags = "验证码模块")
@ApiSupport(author = "lv")
@AllArgsConstructor
public class KaptchaController {

    private final KaptchaUtil kaptcha;

    /**
     * 渲染图形验证码
     * @param key 请求唯一ID,如果不传该参数则由服务端生成并在请求头(key)中返回
     */
    @GetMapping(value = "/render")
    @ApiOperation(value = "渲染图形验证码")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "key",value = "请求唯一ID,如果不传该参数则由服务端生成并在请求头(key)中返回"
            ,example = "4be1434dd8aa41c8aa321ec347fd40be",dataTypeClass = String.class)
    public void render(String key) {
        if (StringUtils.isBlank(key)){
            kaptcha.render();
        }else {
            kaptcha.render(key);
        }
    }

    /**
     * 验证图形验证码
     * @param validCodeDTO 校验图形验证码参数对象
     * @return R
     */
    @PostMapping("/valid")
    @ApiOperation("验证图形验证码接口")
    @ApiOperationSupport(order = 2)
    public R<Object> valid(@RequestBody ValidCodeDTO validCodeDTO){
        ResponseEnum statusEnum = kaptcha.valid(validCodeDTO);
        return R.build(statusEnum);
    }


}
